﻿using Aop.Api;
using Aop.Api.Domain;
using Aop.Api.Request;
using BCGM.MedicalWisdom.Controllers;
using BCGM.MedicalWisdom.Model;
using BCGM.MedicalWisdom.Model.ViewModels.Order;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System.Text;

namespace BCGM.MedicalWisdom.Api.Controllers.Orders
{
    /// <summary>
    /// 后台管理系统-药品在线支付
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class AlipayController : BaseApiController
    {
        /// <summary>
        /// 定义私有支付Options接口
        /// </summary>
        private readonly IOptions<AlipayConfig> _alipayConfig;
        /// <summary>
        /// 在线支付构造函数
        /// </summary>
        /// <param name="alipayConfig"></param>
        public AlipayController(IOptions<AlipayConfig> alipayConfig)
        {
            _alipayConfig = alipayConfig;
        }
        /// <summary>
        /// 支付宝在线沙箱支付
        /// </summary>
        /// <param name="alipay"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Alipay([FromQuery] Alipay alipay)
        {
            //创建一个支付宝的客户端（配置文件从appsetting.json读取）
            IAopClient aopClient = new DefaultAopClient(_alipayConfig.Value);
            //创建一个页面支付的请求 Page Pay Request
            AlipayTradePagePayRequest request= new AlipayTradePagePayRequest();
            
            request.SetReturnUrl("");
            request.SetNotifyUrl("");

            AlipayTradePagePayModel model=new AlipayTradePagePayModel();

            model.OutTradeNo = alipay.ONum;             //订单编号
            model.TotalAmount = alipay.GoodsSumMoney;   //购买金额
            model.Subject = alipay.Title;               //商品名称
            model.ProductCode = "FAST_INSTANT_TRADE_PAY";
            //把模型数据设定到请求对象中
            request.SetBizModel(model);
            //执行支付请求
            var response=aopClient.pageExecute(request);
            //  Encoding.UTF8 获取 UTF-8 格式的编码
            return Content(response.Body, "text/html", Encoding.UTF8);
        }
    }
}
